package com.example.helloworld;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

@SuppressLint("JavascriptInterface")
public class Webview extends Activity {

	private WebView webView;
	private Button button;
	private Handler handler =	new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);
		
		webView =	(WebView)findViewById(R.id.webView);
		
		WebSettings settings =	webView.getSettings();
		settings.setSavePassword(false);
		settings.setSaveFormData(false);
		settings.setJavaScriptEnabled(true);
		settings.setSupportZoom(false);
		
		webView.setWebChromeClient(new WebBrowserClient());
		webView.addJavascriptInterface(new JavaScriptMethods(), "sample");
		webView.loadUrl("file:///android_asset/sample.html");
		
		final EditText edit =	(EditText)findViewById(R.id.editUrl);
		button =				(Button)findViewById(R.id.go);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				webView.loadUrl(edit.getText().toString());
			}
		});
	}
	
	final class JavaScriptMethods {
		JavaScriptMethods() {}
		
		public void clickOnFace() {
			handler.post(new Runnable() {
				public void run() {
					button.setText("Open after click!");
					webView.loadUrl("javascript:changeFace();");
				}
			});
		}
	}

	final class WebBrowserClient extends WebChromeClient {
		@Override
		public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
			Log.d("-", message);
			result.confirm();
			
			return true;
		}
	}
}
